﻿package mortal.game.view.copy.crossMainCity
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import extend.language.*;
    import fl.data.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.utils.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.crossMainCity.panels.*;
    import mortal.game.view.copy.crossMainCity.panels.data.*;
    import mortal.game.view.copy.crossMainCity.panels.sub.*;
    import mortal.mvc.core.*;

    public class MainCityBattleController extends CopyControllerBase
    {
        private var _module:MainCityModule;
        private var _koInfoPanel:MainCityBattleKOInfoPanel;
        private var _finalInfoPanel:MainCityBattleFinalInfoPanel;
        private var _rankPanel:MainCityRankPanel;

        public function MainCityBattleController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.NavbarButtonUIRowInited, this.uiGotHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicOpenMainCityBattle, this.battleOpenedHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCloseMainCityBattle, this.battleClosedHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicMainCityBattleGuildScore, this.guildKORankHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicMainCityBattleResult, this.allPlayersRankHandler);
            Dispatcher.addEventListener(EventName.MainCityKOGuildWinAhead, this.guildWinAheadHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicMainCityOpen, this.mainCityOpen);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicMainCityClose, this.mainCityClose);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicMainCityBattle2GuildScore, this.guildFinalRankHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicMainCityBattle2Result, this.allPlayersRankHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicMainCityBattleOpenToday, this.todayOpenBattleOrNotHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicMainCityBattle2PickFlag, this.finalPickFlagHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicMainCityBattle2LostFlag, this.finalLostFlagHandler);
            Dispatcher.addEventListener(EventName.MainCityModuleOpenTabIndex, this.openModuleTabIndex);
            Dispatcher.addEventListener(EventName.MainCityModuleOpenCurEnterPage, this.openCurBattleEnterPageHandler);
            Dispatcher.addEventListener(EventName.MainCityRankPanelOpenIndex, this.opanRankPanelTabIndexHandler);
            Dispatcher.addEventListener(EventName.MainCityFinalOccupyTimeChanged, this.finalOccupyTimeChangedHandler);
            Dispatcher.addEventListener(EventName.MainCityHideRankPanel, this.hideRankPanel);
            Dispatcher.addEventListener(EventName.NavbarButtonUIRowInited, this.sceneUpdateHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicMainCityRevenueProfit, this.revenueCmdHandler);
            NetDispatcher.addCmdListener(ServerCommand.MainCityRevenueInfoGot, this.updateRevenueInfo);
            return;
        }// end function

        private function sceneUpdateHandler(event:DataEvent) : void
        {
            Dispatcher.removeEventListener(EventName.NavbarButtonUIRowInited, this.sceneUpdateHandler);
            GameProxy.guild.getMainCityRevenueInfo();
            GameProxy.guild.getMainCityPostionInfo();
            return;
        }// end function

        private function revenueCmdHandler(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SMainCityRevenueMsg;
            cache.mainCity.revenueInfo = _loc_2;
            this.updateRevenueInfo();
            return;
        }// end function

        private function updateRevenueInfo(param1 = null) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.MainCityTaxUpdate));
            if (this._module == null || this._module.isHide)
            {
                return;
            }
            this._module.updateRevenueInfo(cache.mainCity.revenueInfo);
            return;
        }// end function

        private function mainCityOpen(param1) : void
        {
            MainCityIcon.instance.show();
            return;
        }// end function

        private function mainCityClose(param1) : void
        {
            MainCityIcon.instance.hide();
            return;
        }// end function

        private function guildWinAheadHandler(param1) : void
        {
            var _loc_2:* = ClockManager.instance.nowDate.hours * 3600 + ClockManager.instance.nowDate.minutes * 60 + ClockManager.instance.nowDate.seconds;
            var _loc_3:* = cache.mainCity.openInfos.openDt;
            var _loc_4:int = 20;
            var _loc_5:* = _loc_3.hours * 3600 + (_loc_3.minutes + _loc_4) * 60 + _loc_3.seconds - _loc_2;
            if (_loc_5 > 0)
            {
                MainCityBattleTips.instance.show();
                MainCityBattleTips.instance.showText(Language.getString(20965), _loc_5);
            }
            else if (MainCityBattleTips.hasShowingInstance())
            {
                MainCityBattleTips.instance.hide();
            }
            return;
        }// end function

        private function todayOpenBattleOrNotHandler(param1:MessageBlock) : void
        {
            cache.mainCity.isOpenBattleToday = (param1.messageBase as SPublicBool).publicBool;
            return;
        }// end function

        private function updateMainCityIcon(param1) : void
        {
            var _loc_2:* = Cache.instance.mainCity.getPostionByPosType(EMainCityPosition._EMainCityKing);
            if (_loc_2 == null || _loc_2.officer == null)
            {
                if (MainCityIcon.hasShowingIcon())
                {
                    MainCityIcon.instance.hide();
                }
            }
            else
            {
                MainCityIcon.instance.show();
            }
            return;
        }// end function

        private function finalOccupyTimeChangedHandler(event:DataEvent) : void
        {
            this.updateFinalCopyInfoPanel();
            return;
        }// end function

        private function finalPickFlagHandler(param1:MessageBlock) : void
        {
            MainCityFinalOccupyTime.instance.pickFlag(param1.messageBase as SMainCityBattle2MiniGuildMsg);
            return;
        }// end function

        private function finalLostFlagHandler(param1:MessageBlock) : void
        {
            MainCityFinalOccupyTime.instance.lostFlag();
            return;
        }// end function

        private function opanRankPanelTabIndexHandler(event:DataEvent) : void
        {
            var _loc_2:* = int(event.data);
            this.opanRankPanelTabIndex(_loc_2);
            return;
        }// end function

        private function opanRankPanelTabIndex(param1:int) : void
        {
            if (this._rankPanel == null)
            {
                this._rankPanel = new MainCityRankPanel();
            }
            this._rankPanel.show();
            this._rankPanel.changeToTabIndex(param1);
            return;
        }// end function

        private function hideRankPanel(param1 = null) : void
        {
            if (this._rankPanel != null && !this._rankPanel.isHide)
            {
                this._rankPanel.hide();
            }
            return;
        }// end function

        override public function leaveCopy() : void
        {
            super.leaveCopy();
            if (MainCityFinalOccupyTime.hasInstance)
            {
                MainCityFinalOccupyTime.instance.dispose();
            }
            if (this._finalInfoPanel != null && !this._finalInfoPanel.isHide)
            {
                this._finalInfoPanel.hide();
            }
            if (this._koInfoPanel != null && !this._koInfoPanel.isHide)
            {
                this._koInfoPanel.hide();
            }
            cache.role.roleEntityInfo.clearMainCityFlag();
            return;
        }// end function

        override public function enterCopy(param1:TCopy) : void
        {
            super.enterCopy(param1);
            if (this._module != null && !this._module.isHide)
            {
                this._module.hide();
            }
            if (CopyUtil.isInMainCityBattleKOCopy)
            {
                MainCityBattleKORuleWin.instance.show();
                if (this._koInfoPanel == null)
                {
                    this._koInfoPanel = new MainCityBattleKOInfoPanel();
                }
                this._koInfoPanel.show();
                this.updateKOCopyInfoPanel();
                cache.mainCity.lastInFinal = false;
            }
            else if (CopyUtil.isInMainCityBattleFinalCopy)
            {
                MainCityBattleFinalRuleWin.instance.show();
                if (this._finalInfoPanel == null)
                {
                    this._finalInfoPanel = new MainCityBattleFinalInfoPanel();
                }
                this._finalInfoPanel.show();
                this.updateFinalCopyInfoPanel();
                cache.mainCity.lastInFinal = true;
            }
            return;
        }// end function

        private function updateFinalCopyInfoPanel(param1:Boolean = false) : void
        {
            if (this._finalInfoPanel == null || this._finalInfoPanel.isHide)
            {
                return;
            }
            var _loc_2:* = cache.mainCity.finalBattleGuildRankData;
            if (_loc_2 == null)
            {
                return;
            }
            if (param1 && _loc_2.leftSeconds > 0)
            {
                this._finalInfoPanel.updateLeftTime(_loc_2.leftSeconds);
            }
            this._finalInfoPanel.updateRank(cache.mainCity.getFinalBattleGuildRankDataProvider());
            var _loc_3:* = cache.mainCity.getMyGuildRankInfo();
            if (_loc_3 != null)
            {
                this._finalInfoPanel.updateCanGetFlagName(_loc_3.canCarryFlagPlayerName, _loc_3.canCarryFlagPlayerType);
            }
            return;
        }// end function

        private function updateKOCopyInfoPanel() : void
        {
            var _loc_4:SMainCityBattleGuildScore = null;
            var _loc_5:MainCityBattleGuildRankData = null;
            if (this._koInfoPanel == null || this._koInfoPanel.isHide)
            {
                return;
            }
            var _loc_1:* = cache.mainCity.koBattleGuildRankData;
            if (_loc_1 == null)
            {
                return;
            }
            this._koInfoPanel.updateFlagNum(cache.mainCity.getKOLeftFlagNum());
            var _loc_2:* = new DataProvider();
            var _loc_3:int = 0;
            while (_loc_3 < _loc_1.battleScores.length)
            {
                
                _loc_4 = _loc_1.battleScores[_loc_3];
                _loc_5 = new MainCityBattleGuildRankData();
                _loc_5.rank = _loc_3 + 1;
                _loc_5.name = _loc_4.guildName;
                _loc_5.value = _loc_4.score;
                _loc_2.addItem(_loc_5);
                _loc_3++;
            }
            this._koInfoPanel.updateRank(_loc_2);
            if (_loc_1.timeLeft > 0)
            {
                this._koInfoPanel.updateLeftTime(_loc_1.timeLeft);
            }
            return;
        }// end function

        private function guildKORankHandler(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SMainCityBattleScore;
            cache.mainCity.koBattleGuildRankData = _loc_2;
            this.updateKOCopyInfoPanel();
            return;
        }// end function

        private function allPlayersRankHandler(param1:MessageBlock) : void
        {
            cache.mainCity.finalPlayersRankData = param1.messageBase as SMainCityBattle2PlayerResultMsg;
            this.opanRankPanelTabIndex(0);
            return;
        }// end function

        private function guildFinalRankHandler(param1:MessageBlock) : void
        {
            cache.mainCity.finalBattleGuildRankData = param1.messageBase as SMainCityBattle2ScoresMsg;
            this.updateFinalCopyInfoPanel(true);
            return;
        }// end function

        private function openModuleTabIndex(event:DataEvent) : void
        {
            var _loc_2:* = int(event.data);
            this.showModule(true);
            this._module.changeToTabIndexByName(_loc_2);
            return;
        }// end function

        private function openCurBattleEnterPageHandler(param1) : void
        {
            var _loc_2:* = MainCityModule.Index_FinalPanel;
            if (Cache.instance.mainCity.isKOBattleDuration)
            {
                _loc_2 = MainCityModule.Index_KOPanel;
            }
            this.showModule(true);
            this._module.changeToTabIndexByName(_loc_2);
            return;
        }// end function

        private function showModule(param1:Boolean = true) : void
        {
            if (param1)
            {
                if (this._module == null)
                {
                    this._module = new MainCityModule();
                }
                if (this._module.isHide)
                {
                    this._module.show();
                }
            }
            else if (this._module != null && !this._module.isHide)
            {
                this._module.hide();
            }
            return;
        }// end function

        private function battleOpenedHandler(param1:MessageBlock) : void
        {
            var _loc_3:int = 0;
            var _loc_2:* = param1.messageBase as SMainCityBattleOpenMsg;
            cache.mainCity.openInfos = _loc_2;
            if (_loc_2 == null)
            {
                return;
            }
            if (_loc_2.endDt.time < ClockManager.instance.nowDate.time)
            {
                return;
            }
            MainCityBattleIcon.instance.show();
            if (_loc_2.openDt != null)
            {
                _loc_3 = Math.ceil((_loc_2.openDt.time - ClockManager.instance.nowDate.time) / 1000);
                if (_loc_3 > 0)
                {
                    MainCityBattleIcon.instance.setLeftTimeAndState(_loc_3, 1);
                }
                else if (_loc_2.endDt != null)
                {
                    _loc_3 = Math.ceil((_loc_2.endDt.time - ClockManager.instance.nowDate.time) / 1000);
                    MainCityBattleIcon.instance.setLeftTimeAndState(_loc_3, 2);
                }
            }
            return;
        }// end function

        private function uiGotHandler(param1 = null) : void
        {
            if (cache.role.roleEntityInfo.entityInfo.level >= 60)
            {
                MainCityBattleIcon.instance.show();
            }
            return;
        }// end function

        private function battleClosedHandler(param1:MessageBlock) : void
        {
            if (MainCityBattleIcon.hasShowingIcon())
            {
                MainCityBattleIcon.instance.hide();
            }
            return;
        }// end function

    }
}
